package com.zgw.his.emr.suport;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.e4.xwt.DefaultLoadingContext;
import org.eclipse.e4.xwt.IXWTLoader;
import org.eclipse.e4.xwt.XWT;
import org.eclipse.e4.xwt.core.IUserDataConstants;
import org.eclipse.e4.xwt.forms.XWTForms;
import org.eclipse.e4.xwt.internal.core.ScopeKeeper;
import org.eclipse.e4.xwt.internal.utils.UserData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * XWT UI 构建支持类
 * 
 * @author Z.G.W
 * */
public class XwtSuport extends XWT {
	static {
		setLoadingContext(new DefaultLoadingContext(
				XwtSuport.class.getClassLoader())); // 设置 XWT 类加载器
	}

	public static Control buildUI(Composite parent, URL url) throws Exception {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put(IXWTLoader.CLASS_PROPERTY, new BaseAssist());
		options.put(IXWTLoader.CONTAINER_PROPERTY, parent);
		return XWT.loadWithOptions(url, options);
	}

	// 查找元素名称
	public static String getElementName(Control maternal, Control con) {
		String name = null;
		if (maternal.getData(IUserDataConstants.XWT_USER_DATA_KEY) instanceof UserData) {
			UserData userdata = (UserData) maternal
					.getData(IUserDataConstants.XWT_USER_DATA_KEY);
			ScopeKeeper nameContext = (ScopeKeeper) userdata
					.getData(IUserDataConstants.XWT_NAMECONTEXT_KEY);
			for (String n : nameContext.names()) {
				if (con.equals(nameContext.getNamedObject(n))) {
					name = n;
					break;
				}
			}
		}
		return name;
	}
}
